/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SalidaEntrada.java
 *
 * Created on 21/03/2012, 04:13:00 PM
 */
package com.torresquintero.oncologos.quimioterapia.vista;

import com.torresquintero.oncologos.common.vista.OncologosTableModel;
import com.torresquintero.oncologos.common.vista.PanelOncologos;
import com.torresquintero.oncologos.entidades.Inventario;
import com.torresquintero.oncologos.secretaria.controladores.KardexController;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author diana
 */
public class PanelSalidaEntrada extends PanelOncologos {

    private KardexController control;

    /** Creates new form SalidaEntrada */
    public PanelSalidaEntrada() {
        initComponents();
        control = KardexController.getInstance();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTxCodArticulo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaInventario = new javax.swing.JTable();
        bGuardar = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        bEntrada = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        bSalida = new com.torresquintero.oncologos.common.vista.BotonOncologos();
        jButton1 = new javax.swing.JButton();
        tfCodigoBarras = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        jLabel1.setText("Codigo Articulo:");

        tablaInventario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Codigo Articulo", "Codigo de Barras", "Nombre", "Fecha Entrada", "Fecha Salida", "Responsable entrada", "Responsable Salida"
            }
        ));
        jScrollPane1.setViewportView(tablaInventario);

        bGuardar.setText("Terminar");
        bGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardarActionPerformed(evt);
            }
        });

        bEntrada.setText("Entrada");
        bEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEntradaActionPerformed(evt);
            }
        });

        bSalida.setText("Salida");
        bSalida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSalidaActionPerformed(evt);
            }
        });

        jButton1.setText("Eliminar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Codigo de Barras");

        jButton2.setText("Buscar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tfCodigoBarras, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxCodArticulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bSalida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton2))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxCodArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfCodigoBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(bEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bSalida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        
        String codigo =(String) tablaInventario.getValueAt(tablaInventario.getSelectedRow(), 1);
        control.eliminarLista(codigo);
        llenarTabla();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed
        // TODO add your handling code here:
        int opc = JOptionPane.showConfirmDialog(null, "Desea guardar los cambios al inventario?");
        if (opc == JOptionPane.YES_OPTION) {
            control.terminar();
            control=KardexController.getInstance();
            llenarTabla();
        }
}//GEN-LAST:event_bGuardarActionPerformed

    private void bSalidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSalidaActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            
            control.sacarDelInventario(tfCodigoBarras.getText());
            llenarTabla();
        } catch (Exception ex) {
            Logger.getLogger(PanelSalidaEntrada.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_bSalidaActionPerformed

    private void bEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEntradaActionPerformed
        try {
            // TODO add your handling code here:
            
            control.agregaralInventario(jTxCodArticulo.getText(), tfCodigoBarras.getText());
            llenarTabla();
        } catch (Exception ex) {
            Logger.getLogger(PanelSalidaEntrada.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_bEntradaActionPerformed

    public void llenarTabla() {
        List<Inventario> lista = control.getListaInventario();
        String titulos = "Codigo Articulo, Codigo de Barras,Nombre,Fecha Ingreso,"
                + "FEcha Salida,Responsable Ingreso,Responsable Salida.";

        String campos = "articulo.codigo,codigoBarras,articulo.nombre,fechaIngreso,"
                + "fechaSalida,funcionarioIngreso.nombre,"
                + "funcionarioSalida.nombre";

        OncologosTableModel tabla = new OncologosTableModel(lista, campos, titulos);
        tablaInventario.setModel(tabla);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.torresquintero.oncologos.common.vista.BotonOncologos bEntrada;
    private com.torresquintero.oncologos.common.vista.BotonOncologos bGuardar;
    private com.torresquintero.oncologos.common.vista.BotonOncologos bSalida;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTxCodArticulo;
    private javax.swing.JTable tablaInventario;
    private javax.swing.JTextField tfCodigoBarras;
    // End of variables declaration//GEN-END:variables
}
